package org.jin.samples.web;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jin.samples.model.CheckUser;
import org.jin.samples.model.ExamScore;
import org.jin.samples.model.ExamSubject;
import org.jin.samples.model.vo.ExamUserVO;
import org.jin.samples.service.IScoreService;
import org.jin.samples.service.ISubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

@Controller
public class CheckController {
	@Autowired
	private ISubjectService subjectService;
	
	@Autowired
	private IScoreService scoreService;
	
	public String checkScore(Model model)throws Exception{
		List<ExamSubject> subjectList = subjectService.findAllSubject();
		model.addAttribute("subjectList", subjectList);
		CheckUser checkUser = new CheckUser();
		model.addAttribute("checkUser", checkUser);
		return "score/checkScore";
	}
	
	public String tocheckScore(Model model, CheckUser checkUser)throws Exception{
		List<ExamScore> list = new ArrayList<ExamScore>();
		if(checkUser.getTimeOne() !="" && checkUser.getTimeTwo() != ""){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date date1 = (Date) sdf.parseObject(checkUser.getTimeOne());
			Date date2 = (Date) sdf.parseObject(checkUser.getTimeTwo());
			if(date1.getTime()>date2.getTime()){
				String d = checkUser.getTimeTwo();
				checkUser.setTimeTwo(checkUser.getTimeOne());
				checkUser.setTimeOne(d);
			}
		}
		
		list = scoreService.checkUser(checkUser);
		List<ExamUserVO> subjectList = subjectService.findAllStudent();
		model.addAttribute("subjectList",subjectList);
		model.addAttribute("userList", list);
		return "score/checkScore";
	}
	
	
}










